package com.nexgo.data.repository;

import com.nexgo.data.entity.mapper.EntityDataMapper;
import com.nexgo.data.exception.RequestException;
import com.nexgo.data.net.SocketComm;
import com.nexgo.domain.repository.PaymentPlatform;
import com.nexgo.iso8583.exception.WriteException;

import io.reactivex.Observable;

/**
 * Created by xiaox on 2017/6/2.
 */

public class SocketPayment implements PaymentPlatform {

    private final SocketComm mSocketComm;

    public SocketPayment(String ip, int port, int timeout) {
        mSocketComm = new SocketComm(ip, port, timeout);
    }

    @Override
    public Observable<Object> trans(Object msg) {
        final EntityDataMapper mapper = new EntityDataMapper();
        try {
            return mSocketComm.sendAndRecv(mapper.transform(msg)).map(bytes -> mapper.transform(bytes));
        } catch (WriteException e) {
            e.printStackTrace();
            return Observable.error(new RequestException("组包失败"));
        }

    }

//    @Override
//    public Observable<MessageData> trans(JsonSendData params) {
//        return null;
//    }

}
